package com.ruoyi.system.service.constructionLike.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.constructionLike.ConstructionCollected;
import com.ruoyi.system.domain.constructionLike.ConstructionTags;
import com.ruoyi.system.mapper.constructionLike.ConstructionCollectedMapper;
import com.ruoyi.system.mapper.constructionLike.ConstructionTagsMapper;
import com.ruoyi.system.service.constructionLike.ConstructionCollectedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 施工攻略收藏Service业务层处理
 *
 * @author ruoyi
 * @date 2021-04-11
 */
@Service
public class ConstructionCollectedServiceImpl implements ConstructionCollectedService {
    @Autowired
    private ConstructionCollectedMapper constructionCollectedMapper;
    @Autowired
    private ConstructionTagsMapper constructionTagsMapper;

    /**
     * 查询施工攻略收藏列表
     *
     * @param constructionCollected 施工攻略收藏
     * @return 施工攻略收藏
     */
    @Override
    public ConstructionCollected selectConstructionCollectedList(ConstructionCollected constructionCollected)
    {
        return constructionCollectedMapper.selectConstructionCollectedList(constructionCollected);
    }

    /**
     * 新增施工攻略收藏
     *
     * @param constructionCollected 施工攻略收藏
     * @return 结果
     */
    @Override
    public int insertConstructionCollected(ConstructionCollected constructionCollected)
    {
        String status = constructionCollected.getStatus();
        constructionCollected.setStatus("");
        ConstructionCollected constructionCollected1 = constructionCollectedMapper.selectConstructionCollectedList(constructionCollected);
        if (constructionCollected1 != null && !constructionCollected1.equals("")){
            ConstructionTags constructionTags = new ConstructionTags();
            constructionTags.setId(Long.valueOf(constructionCollected1.getTagsId()));
            constructionTags.setUserId(constructionCollected1.getUserId());
            constructionTags.setConstructionStrategyId(constructionCollected1.getConstructionStrategyId());
            constructionCollected.setUpdateTime(DateUtils.getNowDate());
            constructionCollected.setId(constructionCollected1.getId());
            constructionCollected.setStatus(status);
            int i = constructionTagsMapper.updateConstructionTags(constructionTags);
            if (i > 0){
                int i1 = constructionCollectedMapper.updateConstructionCollected(constructionCollected);
                if (i1 > 0){
                    return 1;
                }
            }
        }else{
            constructionCollected.setUpdateTime(DateUtils.getNowDate());
            ConstructionTags constructionTags = new ConstructionTags();
            constructionTags.setUserId(constructionCollected.getUserId());
            constructionTags.setConstructionStrategyId(constructionCollected.getConstructionStrategyId());
            constructionCollected.setStatus(status);
            ConstructionTags constructionTags1 = constructionTagsMapper.selectConstructionTagsList(constructionTags);
            if (constructionTags1 != null && !constructionTags1.equals("")){
                constructionCollected.setTagsId(Math.toIntExact(constructionTags1.getId()));
                int i1 = constructionCollectedMapper.insertConstructionCollected(constructionCollected);
                if (i1 > 0){
                    return 1;
                }
            }else{
                int i = constructionTagsMapper.insertConstructionTags(constructionTags);
                constructionCollected.setTagsId(Math.toIntExact(constructionTags.getId()));
                if (i > 0){
                    int i1 = constructionCollectedMapper.insertConstructionCollected(constructionCollected);
                    if (i1 > 0){
                        return 1;
                    }
                }
            }
        }
        return 0;
    }

    /**
     * 修改施工攻略收藏
     *
     * @param constructionCollected 施工攻略收藏
     * @return 结果
     */
    @Override
    public int updateConstructionCollected(ConstructionCollected constructionCollected)
    {
        constructionCollected.setUpdateTime(DateUtils.getNowDate());
        return constructionCollectedMapper.updateConstructionCollected(constructionCollected);
    }

    /**
     * 查询所有施工攻略搜藏数量
     *
     * @return 结果
     */
    @Override
    public int selectConstructionCollectedCount(Integer constructionStrategyId) {
        return constructionCollectedMapper.selectConstructionCollectedCount(constructionStrategyId);
    }
}
